#coding:utf-8
'''
@author:        laohu
@date:          2021.07.17
@brief:         资产入库弹出框
@note:          继承自Dialog
'''


from Infrastructure.AssetStatus import AssetStatus
from sqlalchemy.sql.operators import isnot
from ElectronicAssetDAO import ElectronicAssetDAO
from typing import Optional
from PySide6.QtWidgets import QAbstractItemView, QComboBox, QDateEdit, QDialog, QDialogButtonBox, QGridLayout, QHBoxLayout, QHeaderView, QLineEdit, QPushButton, QTableWidget, QTableWidgetItem, QTextEdit, QVBoxLayout, QWidget
from PySide6.QtCore import QDate, Qt
from UIComponents.Dialog.toast import QToast


class WarehousingInDialog(QDialog):
    def __init__(self, parent: Optional[QWidget] = None, f: Qt.WindowFlags = Qt.Dialog) -> None:
        super().__init__(parent, f)
        self.resize(800, 500)
        self.setWindowTitle("资产入库")
        self.glayout = QGridLayout(self)
        self.hlayout = QHBoxLayout()
        self.assetNumberLineEdit = QLineEdit()
        self.assetNumberLineEdit.setPlaceholderText("资产编号")
        self.hlayout.addWidget(self.assetNumberLineEdit)
        self.deviceNumberLineEdit = QLineEdit()
        self.deviceNumberLineEdit.setPlaceholderText("设备编号")
        self.hlayout.addWidget(self.deviceNumberLineEdit)
        self.deviceCategoryLineEdit = QLineEdit()
        self.deviceCategoryLineEdit.setPlaceholderText("设备类型")
        self.hlayout.addWidget(self.deviceCategoryLineEdit)
        self.brandLineEdit = QLineEdit()
        self.brandLineEdit.setPlaceholderText("品牌")
        self.hlayout.addWidget(self.brandLineEdit)
        self.deviceModelLineEdit = QLineEdit()
        self.deviceModelLineEdit.setPlaceholderText("型号")
        self.hlayout.addWidget(self.deviceModelLineEdit)
        self.usingTypeComboBox = QComboBox()
        self.usingTypeComboBox.addItem("n", "n")
        self.usingTypeComboBox.addItem("w", "w")
        self.hlayout.addWidget(self.usingTypeComboBox)
        self.originLineEdit = QLineEdit()
        self.originLineEdit.setPlaceholderText("产地")
        self.hlayout.addWidget(self.originLineEdit)
        self.applicationLineEdit = QLineEdit()
        self.applicationLineEdit.setPlaceholderText("用途")
        self.hlayout.addWidget(self.applicationLineEdit)
        self.productionDateEdit = QDateEdit()
        self.productionDateEdit.setDate(QDate.currentDate())
        self.hlayout.addWidget(self.productionDateEdit)
        self.remarkLineEdit = QLineEdit()
        self.remarkLineEdit.setPlaceholderText("备注")
        self.hlayout.addWidget(self.remarkLineEdit)
        self.addButton = QPushButton("添加")
        self.addButton.clicked.connect(self.addAsset)
        self.hlayout.addWidget(self.addButton)
        self.glayout.addLayout(self.hlayout, 0, 0)
        self.wInAssetsTable = QTableWidget()
        self.wInAssetsTable.setSelectionMode(QAbstractItemView.ExtendedSelection)  # 设置只可以单选，可以使用ExtendedSelection进行多选
        self.wInAssetsTable.setSelectionBehavior(QAbstractItemView.SelectRows)  # 设置 不可选择单个单元格，只可选择一行。
        self.wInAssetsTable.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.wInAssetsTable.setColumnCount(11)
        self.wInAssetsTable.setHorizontalHeaderLabels(
            ['资产编号', '设备编号', '设备类别', '品牌', '型号', '使用类型', '产地', '用途',
            '生产日期','备注','操作'])#设置表头文字
        self.glayout.addWidget(self.wInAssetsTable, 1, 0)
        self.buttons = QDialogButtonBox()
        self.buttons.addButton("确认", QDialogButtonBox.AcceptRole)
        self.buttons.addButton("取消", QDialogButtonBox.RejectRole)
        self.buttons.accepted.connect(self.accept)
        self.buttons.rejected.connect(self.reject)
        self.glayout.addWidget(self.buttons, 2, 0)
        self.setWindowModality(Qt.ApplicationModal)

    def addAsset(self):
        assetNumber = self.assetNumberLineEdit.text()
        for i in range(0, self.wInAssetsTable.rowCount()):
            if assetNumber == self.wInAssetsTable.item(i, 0).text():
                toast = QToast(self, "资产编号已存在于待入库列表", 0, 0, "")
                toast.toast()
                return
        assets = ElectronicAssetDAO.search_device(**{"asset_ID": assetNumber})
        if len(assets) is 0:
            row = self.wInAssetsTable.rowCount()
            self.wInAssetsTable.insertRow(row)
            self.wInAssetsTable.setItem(row, 0, QTableWidgetItem(self.assetNumberLineEdit.text()))
            self.wInAssetsTable.setItem(row, 1, QTableWidgetItem(self.deviceNumberLineEdit.text()))
            self.wInAssetsTable.setItem(row, 2, QTableWidgetItem(self.deviceCategoryLineEdit.text()))
            self.wInAssetsTable.setItem(row, 3, QTableWidgetItem(self.brandLineEdit.text()))
            self.wInAssetsTable.setItem(row, 4, QTableWidgetItem(self.deviceModelLineEdit.text()))
            self.wInAssetsTable.setItem(row, 5, QTableWidgetItem(self.usingTypeComboBox.currentData()))
            self.wInAssetsTable.setItem(row, 6, QTableWidgetItem(self.originLineEdit.text()))
            self.wInAssetsTable.setItem(row, 7, QTableWidgetItem(self.applicationLineEdit.text()))
            self.wInAssetsTable.setItem(row, 8, QTableWidgetItem(self.productionDateEdit.date().toString("yyyy\M\d")))
            self.wInAssetsTable.setItem(row, 9, QTableWidgetItem(self.remarkLineEdit.text()))
            buttonDelete = QPushButton("×")
            buttonDelete.clicked.connect(self.buttonDeleteClicked)
            self.wInAssetsTable.setCellWidget(row, 10, buttonDelete)
            self.assetNumberLineEdit.setText("")
            self.deviceNumberLineEdit.setText("")
            self.deviceCategoryLineEdit.setText("")
            self.brandLineEdit.setText("")
            self.deviceModelLineEdit.setText("")
            self.usingTypeComboBox.setCurrentIndex(0)
            self.originLineEdit.setText("")
            self.applicationLineEdit.setText("")
            self.productionDateEdit.setDate(QDate.currentDate())
            self.remarkLineEdit.setText("")
        else:
            toast = QToast(self, "资产编号已存在", 0, 0, "")
            toast.toast()

    def buttonDeleteClicked(self):
        self.wInAssetsTable.removeRow(self.wInAssetsTable.currentRow())
